package com.example.g7640.controller;


import com.example.g7640.builder.ResultBuilder;
import com.example.g7640.dto.OrderDetailDto;
import com.example.g7640.dto.OrderDto;
import com.example.g7640.dto.OrderQuery;
import com.example.g7640.dto.common.PageResult;
import com.example.g7640.dto.common.Result;
import com.example.g7640.service.OrderService;
import com.example.g7640.util.RoleCheckUtil;
import com.example.g7640.util.SessionLocal;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author  
 * @since 2024-03-18
 */
@RestController
@RequestMapping("/api/order")
public class OrderController {


    @Resource
    OrderService orderService;

    @PostMapping("/createOrder")
    public Result<String> createOrder(@RequestBody OrderDto orderDto){
        orderService.createOrder(orderDto);
        return ResultBuilder.buildSuccess();
    }


    @GetMapping("/get-info")
    public Result<OrderDto> queryById(Integer id){
        OrderDto orderDto = orderService.queryOrderById(id);
        return ResultBuilder.buildSuccess(orderDto);
    }


    @GetMapping("/getAllOrderList")
    public PageResult<List<OrderDto>> getAllOrderList(OrderQuery query){
        return orderService.queryOrder(query);
    }

    @GetMapping("/getVendorOrderList")
    public PageResult<List<OrderDto>> getVendorOrderList(OrderQuery query){
        RoleCheckUtil.checkVendor();
        query.setVid(SessionLocal.getLoginId());
        return orderService.queryOrder(query);
    }

    @GetMapping("/getCustomerOrderList")
    public PageResult<List<OrderDto>> getCustomerOrderList(OrderQuery query){
        RoleCheckUtil.checkCustomer();
        query.setCid(SessionLocal.getLoginId());
        return orderService.queryOrder(query);
    }

    @GetMapping("/cancelOrder")
    public Result<String> cancelOrder(Integer id){
        orderService.cancelOrder(id);
        return ResultBuilder.buildSuccess();
    }

    @GetMapping("/deleteOrder")
    public Result<String> deleteOrder(Integer id){
        orderService.deleteOrder(id);
        return ResultBuilder.buildSuccess();
    }


    @PostMapping("/updateOrderStatus")
    public Result<String> updateOrderStatus(@RequestBody OrderDto orderDto){
        orderService.updateOrderStatus(orderDto);
        return ResultBuilder.buildSuccess();
    }

    @PostMapping("/updateDetailStatus")
    public Result<String> updateDetailStatus(@RequestBody OrderDetailDto orderDetailDto){
        orderService.updateOrderDetail(orderDetailDto);
        return ResultBuilder.buildSuccess();
    }

}
